// src/services/userService.js
import request from './request';

// 处理请求返回的数据
const handleResponse = (response) => {
  console.log('response-->',response);
  const { code, msg = '' } = response;
  if (code !== 0) {
    console.log('msg-->',msg);
    throw new Error(msg);
  }
  return response;
};

// 用户登录，获取token
export const loginUser = async (username, password) => {
  const response = await request(`/login?username=${username}&password=${password}`, 'POST');
  return handleResponse(response);
};

// 分页查询用户
export const fetchUsers = async () => {
  const response = await request('/user/page');
  return handleResponse(response);
};

// 通过id查询用户信息
export const getUser = async (userId) => {
  const response = await request(`/user/${userId}`, 'GET');
  return handleResponse(response);
};

// 添加用户
export const addUser = async (userData) => {
  const response = await request('/user', 'POST', userData);
  return handleResponse(response);
};

// 修改用户信息
export const editUser = async (userData) => {
  const response = await request('/user', 'PUT', userData);
  return handleResponse(response);
};

// 删除用户
export const deleteUser = async (userId) => {
  const response = await request(`/user/${userId}`, 'DELETE');
  return handleResponse(response);
};

// 按照部门统计部门人数
export const countUserbyDept = async()=>{
  const reponse = await request('/user/countDepartment','GET');
  return handleResponse(reponse);
}
